var that,
  api = new(require('../../utils/api.js')),
  staticimg=api.getstaticimg(),
  headimg = api.getimgurl(),
  // md5 = require('../../utils/md5.js'),
  app = getApp(),
  util = new(require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeindex:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    let{id,name} = options;
    that = this;
    wx.showLoading({
      title: '加载中',
    })
    that.getlist(id)
    that.setData({storeid:id,name})
  },
  getlist(id){
    api.keeper_getdetail(id,res=>{
      wx.hideLoading()
      if(res.data.issuccess==1){
        let{list} = res.data,has=!1;
        if(list.length>0){
          if(list[0]['kweight']>0) has=!0
          let allweight = 0,allprice = 0,kallweight=0,kallprice = 0;
          list.forEach(x=>{
            allweight+=x.rwieght;
            allprice+=x.rprice;
            kallweight+=x.kweight;
            kallprice+=x.kprice;
          })
          that.setData({allprice,allweight,kallweight,kallprice})
        }
        that.setData({list,has,})
      }
      
    })
  },
  input(e){
    let{type,index} = e.currentTarget.dataset,{list} = this.data;

    list[index][type] = e.detail.value;
    that.setData({list})
  },
  sumbit(){
    let{list} = this.data,idarr=[],weiarr=[],pricearr=[],finished = true,err='';
    list.forEach(x=>{
     
      if(typeof(x['inputweight'])=='undefined'||typeof(x['inputprice'])=='undefined'){
        finished = false
      }else{
        idarr.push(x.id)
        if(parseInt(x.inputweight*1000)<=0||isNaN(x.inputweight)){
          err = '重量输入有误'
        }
        if(parseInt(x.inputprice*100)<=0||isNaN(x.inputprice)){
          err = '价格输入有误'
        }
        weiarr.push(parseInt(x.inputweight*1000) )
        pricearr.push(parseInt(x.inputprice*100))
      }
    })
    if(!finished){
      util.nocancelmodal('称重或总价未填写完整');
      return;
    }
    if(err!=''){
      util.nocancelmodal(err);
      return;
    }
    console.log('sumbit',idarr);
    console.log('sumbit',weiarr);
    console.log('sumbit',pricearr);


    api.keeper_sumbit({storeid:that.data.storeid,ids:String(idarr),prices:String(pricearr),weights:String(weiarr)},res=>{
      if(res.data.issuccess==1){
        util.nocancelmodal('提交成功','',()=>{
          let page = getCurrentPages();
          page = page[page.length-2];
          page.setData({keepneedfresh:!0},()=>{
            wx.navigateBack()
          })
        })
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})